package com.whfc.fse.queue;

import com.alibaba.fastjson.JSON;
import com.whfc.common.constant.QueueConst;
import com.whfc.common.enums.DelFlag;
import com.whfc.common.enums.NetState;
import com.whfc.fse.dao.FseBridgeDataMapper;
import com.whfc.fse.dao.FseBridgeMapper;
import com.whfc.fse.dto.FseBridgeDataDTO;
import com.whfc.fse.dto.FseWarnDTO;
import com.whfc.fse.entity.FseBridge;
import com.whfc.fse.entity.FseBridgeData;
import com.whfc.fse.entity.FseBridgeDataLog;
import com.whfc.fse.enums.FseType;
import com.whfc.fse.enums.FseWarnRuleType;
import com.whfc.fse.manager.FseWarnMgr;
import com.whfc.influx.dao.FseBridgeDataLogDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 处理架桥机同步的实时数据
 *
 * @author sun_guodong
 * @date 2020-05-26
 */
@Component
@RabbitListener(queuesToDeclare = {@Queue(name = QueueConst.FSE_BRIDGE_CURRENT_DATA)}, concurrency = "1-2",containerFactory = "baseFactory")
public class FseBridgeRealDataQueueListener {
    private static final Logger logger = LoggerFactory.getLogger(FseBridgeRealDataQueueListener.class);

    @Autowired
    private FseBridgeMapper fseBridgeMapper;

    @Autowired
    private FseBridgeDataMapper bridgeDataMapper;

    @Autowired
    private FseWarnMgr fseWarnMgr;

    @Autowired
    private FseBridgeDataLogDao fseBridgeDataLogDao;

    @RabbitHandler
    public void handle(String msg) {
        logger.info("队列同步架桥机实时数据,msg:{}", msg);
        try {
            FseBridgeDataDTO fseBridgeData = JSON.parseObject(msg, FseBridgeDataDTO.class);
            String sn = fseBridgeData.getSn();
            String platform = fseBridgeData.getPlatform();
            FseBridgeData newFseBridgeData = new FseBridgeData();
            BeanUtils.copyProperties(fseBridgeData, newFseBridgeData);
            FseBridge fseBridge = fseBridgeMapper.selectByPlatformAndSn(platform, sn);
            if (fseBridge == null) {
                logger.info("未找到设备sn:{}", sn);
                return;
            }
            Integer frontLiftWarnNum1 = newFseBridgeData.getFrontLiftWarnNum() != null ? newFseBridgeData.getFrontLiftWarnNum() : 0;
            Integer backLiftWarnNum1 = newFseBridgeData.getBackLiftWarnNum() != null ? newFseBridgeData.getBackLiftWarnNum() : 0;
            Integer windWarnNum1 = newFseBridgeData.getWindWarnNum() != null ? newFseBridgeData.getWindWarnNum() : 0;


            FseBridgeDataLog record = new FseBridgeDataLog();
            BeanUtils.copyProperties(newFseBridgeData, record);
            record.setDelFlag(DelFlag.UNDELETE.getValue());
            record.setFrontLiftTimeWarnNum(frontLiftWarnNum1);
            record.setBackLiftTimeWarnNum(backLiftWarnNum1);
            record.setWindTimeWarnNum(windWarnNum1);

            FseBridgeData bridgeData = bridgeDataMapper.selectByBridgeId(fseBridge.getId());
            if (bridgeData == null) {
                bridgeData = new FseBridgeData();
                bridgeData.setBridgeId(fseBridge.getId());
                bridgeDataMapper.insertSelective(bridgeData);
            }
            Integer frontLiftWarnNum2 = bridgeData.getFrontLiftWarnNum() != null ? bridgeData.getFrontLiftWarnNum() : 0;
            Integer frontLiftWarnNum = frontLiftWarnNum1 - frontLiftWarnNum2;
            newFseBridgeData.setFrontLiftTimeWarnNum(frontLiftWarnNum);
            record.setFrontLiftTimeWarnNum(frontLiftWarnNum);


            Integer backLiftWarnNum2 = bridgeData.getBackLiftWarnNum() != null ? bridgeData.getBackLiftWarnNum() : 0;
            Integer backLiftWarnNum = backLiftWarnNum1 - backLiftWarnNum2;
            newFseBridgeData.setBackLiftTimeWarnNum(backLiftWarnNum);
            record.setBackLiftTimeWarnNum(backLiftWarnNum);


            Integer windWarnNum2 = bridgeData.getWindWarnNum() != null ? bridgeData.getWindWarnNum() : 0;
            Integer windWarnNum = windWarnNum1 - windWarnNum2;
            newFseBridgeData.setWindTimeWarnNum(windWarnNum);
            record.setWindTimeWarnNum(windWarnNum);
            // 插入或更新及时数据
            newFseBridgeData.setBridgeId(fseBridge.getId());
            newFseBridgeData.setId(bridgeData.getId());
            bridgeDataMapper.updateByPrimaryKeySelective(newFseBridgeData);

            // 插入历史数据
            record.setBridgeId(fseBridge.getId());
            fseBridgeDataLogDao.insert(record);

            fseBridge.setNetState(NetState.ONLINE.getValue());
            fseBridgeMapper.updateByPrimaryKeySelective(fseBridge);

            //报警处理
            if (fseBridgeData.getWindWarnNum() > 0) {
                FseWarnRuleType fseWarnRuleType = FseWarnRuleType.GANTRY_WIND_SPEED;
                String triggerValue = String.valueOf(fseBridgeData.getWindSpeed());
                this.handleWarn(fseBridge, fseWarnRuleType, triggerValue, fseBridgeData.getTime());
            }
            if (fseBridgeData.getFrontLiftWarn() != null && fseBridgeData.getFrontLiftWarn() > 0) {
                FseWarnRuleType fseWarnRuleType = FseWarnRuleType.BRIDGE_FRONT_OVERLOAD;
                String triggerValue = String.valueOf(fseBridgeData.getFrontLiftWeight());
                this.handleWarn(fseBridge, fseWarnRuleType, triggerValue, fseBridgeData.getTime());
            }

            if (fseBridgeData.getBackLiftWarn() != null && fseBridgeData.getBackLiftWarn() > 0) {
                FseWarnRuleType fseWarnRuleType = FseWarnRuleType.GANTRY_AUX_OVERLOAD;
                String triggerValue = String.valueOf(fseBridgeData.getBackLiftWeight());
                this.handleWarn(fseBridge, fseWarnRuleType, triggerValue, fseBridgeData.getTime());
            }

        } catch (Exception e) {
            logger.error("队列同步架桥机实时数据出错", e);
        }

    }

    /**
     * 处理报警信息
     *
     * @param fseBridge
     * @param fseWarnRuleType
     * @param triggerValue
     * @param triggerTime
     */
    private void handleWarn(FseBridge fseBridge, FseWarnRuleType fseWarnRuleType, String triggerValue, Date triggerTime) {
        FseWarnDTO fseWarnDTO = new FseWarnDTO();
        fseWarnDTO.setFseType(FseType.BRIDGE);
        fseWarnDTO.setFseWarnRuleType(fseWarnRuleType);
        fseWarnDTO.setDeptId(fseBridge.getDeptId());
        fseWarnDTO.setCode(fseBridge.getCode());
        fseWarnDTO.setTriggerObjectId(fseBridge.getId());
        fseWarnDTO.setTriggerTime(triggerTime);
        fseWarnDTO.setTriggerValue(triggerValue);
        fseWarnMgr.handleFseWarn(fseWarnDTO);
    }
}
